#include "yMessage.h"
#include "yNetServer.h"

Message::Message(int opcode) :
		byteArray(DEFAULT_MESSAGE_LEN) {
	byteArray.writeInt(0);
	byteArray.writeShort(opcode);
}

Message::~Message() {
}

void Message::writeByte(char value) {
	byteArray.writeByte(value);
}

void Message::writeShort(short value) {
	byteArray.writeShort(value);
}

void Message::writeInt(int value) {
	byteArray.writeInt(value);
}

void Message::writeLong(long value) {
	byteArray.writeLong(value);
}

void Message::writeFloat(float value) {
	byteArray.writeFloat(value);
}

void Message::writeDouble(double value) {
	byteArray.writeDouble(value);
}

void Message::writeString(std::string& value) {
	byteArray.writeString(value);
}

void Message::writeBytes(char* value, int len) {
	byteArray.writeBytes(value, len);
}

void Message::sendToServer(connection_t ctx) {
	int len = byteArray.writerIndex() - byteArray.readerIndex();
	if (len > 0) {
		int pos = byteArray.writerIndex();
		byteArray.writerIndex(0);
		byteArray.writeInt(len);
		byteArray.writerIndex(pos);
		NetServer::getInstance()->sendData(ctx, byteArray.buf + byteArray.readerIndex(), len);
	}

}
/*
 * yMessage.cpp
 *
 *  Created on: Sep 13, 2016
 *      Author: root
 */

